package com.yz.house;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.jaeger.library.StatusBarUtil;
import com.yz.house.base.BaseActivity;
import com.yz.house.base.BaseFragment;
import com.yz.house.model.TabEntity;
import com.yz.house.ui.adaper.ViewPagerAdapter;
import com.yz.house.ui.fragment.DecorateFragment;
import com.yz.house.ui.fragment.HouseFragment;
import com.yz.house.ui.fragment.MineFragment;
import com.yz.house.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author zhangyf
 * @date 2018/04/12
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.bottom_tab_main)
    CommonTabLayout bottom_tab_main;
    @BindView(R.id.vp_main)
    CustomViewPager vp_main;

    private final String[] mTitles = {"首页", "装修", "我的"};
    private final int[] mIconUnSelectRes = {R.mipmap.tab_home_unsel, R.mipmap.tab_decorate_unsel, R.mipmap.tab_mine_unsel};
    private final int[] mIconSelectRes = {R.mipmap.tab_home_sel, R.mipmap.tab_decorate_sel, R.mipmap.tab_mine_sel};
    private final int START_PAGE_INDEX = 0;
    private ViewPagerAdapter mPagerAdapter;
    private List<BaseFragment> mFragmentList;
    private OnTabSelectListener mOnTabSelectListener;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private long firstTime;

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initVariables(Bundle extras, Bundle savedInstanceState) {

    }

    @Override
    protected void initViews() {
        StatusBarUtil.setTranslucentForImageViewInFragment(MainActivity.this, null);
        initFragments();
        setupViewPager();
    }

    private void initFragments() {
        mFragmentList = new ArrayList<>();
        mFragmentList.add(HouseFragment.newInstance(null));
        mFragmentList.add(DecorateFragment.newInstance(null));
        mFragmentList.add(MineFragment.newInstance(null));
    }

    private void setupViewPager() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectRes[i], mIconUnSelectRes[i]));
        }
        vp_main.setCanScroll(false);
        mPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), mFragmentList, mTitles);
        vp_main.setAdapter(mPagerAdapter);
        vp_main.setOffscreenPageLimit(2);

        bottom_tab_main.setTabData(mTabEntities);

        mOnTabSelectListener = new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                vp_main.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselect(int position) {

            }
        };
        bottom_tab_main.setOnTabSelectListener(mOnTabSelectListener);

        vp_main.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position < mTitles.length) {
                    bottom_tab_main.setCurrentTab(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        vp_main.setCurrentItem(START_PAGE_INDEX);
    }

    @Override
    protected boolean shouldBindEvent() {
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (System.currentTimeMillis() - firstTime > 2000) {
                Toast.makeText(this,"再按一次退出App",Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
